package com.gzyj.server.factory.jtt808.up;

import com.gzyj.data.common.VehicleIncrement;
import com.gzyj.jtt808.v2011.protocol.jtt1078.JTT808_0x1205_AVResouces;
import com.gzyj.jtt808.v2013.HeaderMsgBodyProperty;
import com.gzyj.jtt808.v2013.Package;
import com.gzyj.jtt808.v2013.enums.JT808TerminalResult;
import com.gzyj.jtt808.v2013.protocol.JTT808_0x8001;
import com.gzyj.jtt808.v2013.protocol.jtt1078.JTT808_0x1205;
import com.gzyj.jtt808.v2013.protocol.jtt1078.JTT808_0x9206;
import com.gzyj.pojo.gps.GzyjVideoHistory;
import com.gzyj.server.factory.CommonHandler;
import com.gzyj.server.factory.jtt808.NetWorkSerrvice;
import com.gzyj.server.mapper.GzyjVideoHistoryMapper;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Component
public class JTT808_0x1205_Handler implements CommonHandler {


    @Autowired
    private NetWorkSerrvice netWorkSerrvice;

    @Resource(name = "clusterRedisTemplate")
    private RedisTemplate<String, Object> clusterRedisTemplate;
    @Value("${ftp.server.savePath}")
    private String ftpPath;

    @Value("${ftp.server.ip}")
    private String ftpServerIp;
    @Value("${ftp.server.port}")
    private Integer ftpServerPort;

    @Value("${redis.key.vehicle.cmdrs}")
    private String VehicleZdCMDRS;

    @Autowired
    private GzyjVideoHistoryMapper gzyjVideoHistoryMapper;


    public String generateVerificationCode() {
        Random random = new Random();
        // 首位数字范围1-9
        int firstDigit = random.nextInt(9) + 1;
        // 剩余位数字范围0-9
        StringBuilder sb = new StringBuilder().append(firstDigit);
        for (int i = 0; i < 3; i++) {
            sb.append(random.nextInt(10));
        }
        return sb.toString();
    }

    @Override
    public void handler(ChannelHandlerContext ctx, Package msg) throws Exception {

        JTT808_0x1205 jtt808_0x1205 = (JTT808_0x1205) msg;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        for (JTT808_0x1205_AVResouces avResouces : jtt808_0x1205.getAVResouces()) {

            JTT808_0x9206 jtt8080x9206 = new JTT808_0x9206();

            HeaderMsgBodyProperty headerMsgBodyProperty = new HeaderMsgBodyProperty();
            headerMsgBodyProperty.setSubcontract(msg.getPackageHeader().getMsgBodyProperty().isSubcontract());
            headerMsgBodyProperty.setEncryptMethod(msg.getPackageHeader().getMsgBodyProperty().getEncryptMethod());
            jtt8080x9206.getPackageHeader().setMsgBodyProperty(headerMsgBodyProperty);
            jtt8080x9206.getPackageHeader().setTerminalPhoneNo(msg.getPackageHeader().getTerminalPhoneNo());
            Integer serialNum=Integer.valueOf(generateVerificationCode());
            jtt8080x9206.getPackageHeader().setMsgNum(serialNum);

            jtt8080x9206.setServerIp(ftpServerIp);
            jtt8080x9206.setPort(ftpServerPort);
            jtt8080x9206.setUserName("admin");
            jtt8080x9206.setPassword("123456");
            String path = File.separator + jtt808_0x1205.getPackageHeader().getTerminalPhoneNo() + File.separator + 0x9206 + File.separator + avResouces.getLogicChannelNo()
                    + File.separator + sdf.format(avResouces.getBeginTime()) + File.separator + sdf.format(avResouces.getEndTime());

            jtt8080x9206.setFileUploadPath(path);
            jtt8080x9206.setChannelNo(avResouces.getLogicChannelNo());
            jtt8080x9206.setBeginTime(avResouces.getBeginTime());
            jtt8080x9206.setEndTime(avResouces.getEndTime());
            jtt8080x9206.setAlarmFlag(avResouces.getAlarmFlag());
            jtt8080x9206.setMediaType(avResouces.getAVResourceType());
            jtt8080x9206.setStreamType(avResouces.getStreamType());
            jtt8080x9206.setMemoryType(avResouces.getMemoryType());
            jtt8080x9206.setTaskExcuteCondition((byte) 7);

            netWorkSerrvice.send2Veh(jtt8080x9206.getPackageHeader().getTerminalPhoneNo(), jtt8080x9206.encode());

            GzyjVideoHistory videoHistory=new GzyjVideoHistory();
            videoHistory.setSimNo(jtt808_0x1205.getPackageHeader().getTerminalPhoneNo());
            videoHistory.setLogicChannelNo(avResouces.getLogicChannelNo().intValue());
            videoHistory.setBeginTime(avResouces.getBeginTime());
            videoHistory.setEndTime(avResouces.getEndTime());
            videoHistory.setAlarmFlag(avResouces.getAlarmFlag());
            videoHistory.setAVResourceType(avResouces.getAVResourceType().intValue());
            videoHistory.setStreamType(avResouces.getStreamType().intValue());
            videoHistory.setMemoryType(avResouces.getMemoryType().intValue());
            videoHistory.setFileSize(avResouces.getFileSize().intValue());
            videoHistory.setSerialNum(serialNum);

            gzyjVideoHistoryMapper.insertOrUpdate(videoHistory);

        }

        clusterRedisTemplate.opsForValue().set(VehicleZdCMDRS + jtt808_0x1205.getPackageHeader().getTerminalPhoneNo() + ":" + jtt808_0x1205.getMsgNum(), jtt808_0x1205,10, TimeUnit.MINUTES);
        JTT808_0x8001 jtt808_0x8001 = new JTT808_0x8001();
        HeaderMsgBodyProperty headerMsgBodyProperty = new HeaderMsgBodyProperty();
        headerMsgBodyProperty.setSubcontract(msg.getPackageHeader().getMsgBodyProperty().isSubcontract());
        headerMsgBodyProperty.setEncryptMethod(msg.getPackageHeader().getMsgBodyProperty().getEncryptMethod());
        jtt808_0x8001.getPackageHeader().setMsgBodyProperty(headerMsgBodyProperty);
        jtt808_0x8001.getPackageHeader().setTerminalPhoneNo(msg.getPackageHeader().getTerminalPhoneNo());

        jtt808_0x8001.setMsgNum(msg.getPackageHeader().getMsgNum());
        jtt808_0x8001.setAckMsgId(msg.getPackageHeader().getMsgId());
        jtt808_0x8001.setResult(JT808TerminalResult.成功_确认);

        netWorkSerrvice.send2Veh(jtt808_0x8001.getPackageHeader().getTerminalPhoneNo(), jtt808_0x8001.encode());
    }

    @Override
    public void handler(ChannelHandlerContext ctx, com.gzyj.jtt808.v2019.Package msg) throws Exception {

    }

    @Override
    public void handler(ChannelHandlerContext ctx, com.gzyj.jtt808.v2011.Package msg) {

    }

    @Override
    public void handler(VehicleIncrement vehicleInfo, String msg) {

    }
}
